package org.yagnus.stats.samplers;

/*
 * This rng converts random bits into integers longs boolean/float, etc.
 * 
 * TODO: Implement nextInt(int n) and nextGaussian();
 */
public abstract class RandomNumberGeneratorFromBits extends
		RandomNumberGeneratorShuffle {

	// Deriving class should implement these.
	public abstract boolean nextBoolean();

	public abstract void setSeed(long seed);

	public void nextBytes(byte[] bytes) {
		for (int i = 0; i < bytes.length; ++i) {
			int pattern = 0;
			for (int j = 0; j < 8; ++j)
				if (nextBoolean())
					pattern &= 1 << j;
			bytes[i] = (byte) pattern;
		}
	}

	public int nextInt() {
		int pattern = 0;
		for (int j = 0; j < 32; ++j)
			if (nextBoolean())
				pattern &= 1 << j;

		return pattern;
	}

	public long nextLong() {
		long pattern = 0;
		for (int i = 0; i < 64; ++i) {
			if (nextBoolean())
				pattern &= 1l << i;
		}
		return pattern;
	}

	public float nextFloat() {
		int pattern = 0;
		for (int j = 0; j < 24; ++j)
			if (nextBoolean())
				pattern &= 1 << j;
		return pattern / ((float) (1 << 24));
	}

	public double nextDouble() {
		long pattern = 0;
		for (int j = 0; j < 53; ++j)
			if (nextBoolean())
				pattern &= 1 << j;
		return pattern / ((double) (1l << 53));
	}
}
